package be.ac.ua.pats.thesisrobotica.robot.parser;

//~--- non-JDK imports --------------------------------------------------------

import be.ac.ua.pats.thesisrobotica.robot.constants.CommunicationConstants;
import be.ac.ua.pats.thesisrobotica.robot.constants.RobotConstants;
import be.ac.ua.pats.thesisrobotica.robot.exceptions.InitializationException;
import be.ac.ua.pats.thesisrobotica.robot.exceptions.InvalidConnectionException;
import be.ac.ua.pats.thesisrobotica.robot.exceptions.RobotInitializationException;
import be.ac.ua.pats.thesisrobotica.robot.exceptions.SensorNotFoundException;
import be.ac.ua.pats.thesisrobotica.robot.main.Robot;
import java.io.IOException;

import lejos.nxt.comm.RConsole;

//~--- JDK imports ------------------------------------------------------------

import java.util.StringTokenizer;

/**
 *
 * @author Admin
 */
public class MessageParser {
    private Robot robot;
    private StringTokenizer tokenizer;

    public MessageParser() {}

    public MessageParser(Robot robot) {
        this.robot = robot;
    }

    public void parse(String message) throws InterruptedException, InvalidConnectionException, SensorNotFoundException, IOException, InitializationException, RobotInitializationException {
        RConsole.println("Going to parse: " + message);

        tokenizer = new StringTokenizer(message, ",");

        RConsole.println("Amount of tokens: " + tokenizer.countTokens());

        if ((tokenizer.countTokens() == 3) || (tokenizer.countTokens() == 4)) {
            String[] tokenString = new String[4];

            tokenString[0] = tokenizer.nextToken();
            tokenString[1] = tokenizer.nextToken();
            tokenString[2] = tokenizer.nextToken();

            if (tokenizer.hasMoreTokens()) {
                tokenString[3] = tokenizer.nextToken();
            }

            if (tokenString[0].equals(CommunicationConstants.FirstToken.GET)) {
                parseGet(tokenString);
            } else if (tokenString[0].equals(CommunicationConstants.FirstToken.SET)) {
                parseSet(tokenString);
            }

               // @todo: Correctly handle a received notification

            return;
        }
    }

    public void parseGet(String[] tokenString)
            throws InterruptedException, InvalidConnectionException, SensorNotFoundException, IOException, InitializationException, RobotInitializationException {
        RConsole.println("Parsing a GET");

        if (tokenString[1].equals(CommunicationConstants.SecondToken.ID)) {
            if (tokenString[2].equals(CommunicationConstants.ThirdToken.ROBOT)) {
                RConsole.println("Going to send robot-ID: " );
                RConsole.println("its ok");
                robot.sendId();
            } else if (tokenString[2].equals(CommunicationConstants.ThirdToken.ALL_SENSORS)) {
                RConsole.println("Going to send sensor-ID's");
                robot.sendSensors();
            } 
        } else if(tokenString[1].equals(CommunicationConstants.SecondToken.ID+
                CommunicationConstants.SecondToken.TYPE)) {
            RConsole.println("Going to send sensors (ID+TYPE)");
            robot.sendSensors();
        } else if (tokenString[1].equals(CommunicationConstants.SecondToken.VALUE)) {
            RConsole.println("Going to send sensor-value for sensor "+tokenString[2]);
            robot.sendValue(tokenString[2]);
        } else if (tokenString[1].equals(CommunicationConstants.SecondToken.BENCHMARK)) {
            RConsole.println("Going to send sensor-benchmark for sensor "+tokenString[2]);
            robot.sendBenchmark(tokenString[2]);
        }
    }

    public void parseSet(String[] tokenString) throws SensorNotFoundException, InitializationException, RobotInitializationException {
        RConsole.println("Parsing a SET");

        if (tokenString[1].equals(CommunicationConstants.SecondToken.VALUE)) {
            RConsole.println("Going to set value: " + tokenString[2] + " on " + tokenString[3]);
            robot.setValue(tokenString[2], tokenString[3]);
        } else if (tokenString[1].equals(CommunicationConstants.SecondToken.INTERVAL)) {
            RConsole.println("Going to set interval: " + tokenString[2] + " on " + tokenString[3]);
            robot.setInterval(tokenString[2], tokenString[3]);
        } else if (tokenString[1].equals(CommunicationConstants.SecondToken.THRESHOLD)) {
            RConsole.println("Going to set threshold: " + tokenString[2] + " on " + tokenString[3]);
            robot.setThreshold(tokenString[2], tokenString[3]);
        } else if (tokenString[1].equals(CommunicationConstants.SecondToken.NOTIFICATIONS)) {
            RConsole.println(tokenString[2]);
            if(tokenString[2].equals(CommunicationConstants.ThirdToken.ON)) {
                            RConsole.println("Going to set notifications on");

                robot.setNotifications(true);
            } else if (tokenString[2].equals(CommunicationConstants.ThirdToken.OFF)) {
                                            RConsole.println("Going to set notifications off");
                robot.setNotifications(false);
            }
        }
    }
}
